﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Yeetop.PayHub.ChinaBank;
using Yeetop.PayHub.EToPay;
using Yeetop.PayHub.YeePay;

namespace Yeetop.PayHub
{
    /// <summary>
    /// 在线支付管理。
    /// Copyright © Yeetop Inc.(www.yeetop.com)  All rights reserved. 
    /// </summary>
    /// <history>
    /// [JianTao] 2009-12-31 Created 
    /// [JianTao] 2010-01-01 Modified 
    /// [JianTao] 2010-01-03 Modified 
    /// </history>
    public class PayManager
    {
        /// <summary>
        /// 在线支付.
        /// </summary>
        /// <param name="trade">在线支付交易信息。</param>
        /// <history>
        /// [JianTao] 2010-01-01 Created 
        /// </history>
        public static void PaySend(MerchantInfo merchant, ITradeInfo trade)
        {
            switch (trade.PayPlatform)
            {
                case PayPlatformType.ChinaBank:
                    new ChinaBankPayment().PaySend(merchant, trade);
                    break;
                case PayPlatformType.EToPay:
                    new EToPayPayment().PaySend(merchant, trade);
                    break;
                case PayPlatformType.YeePay:
                    new YeePayPayment().PaySend(merchant, trade);
                    break;
            }
        }

        public static ReceiveDataInfo PayReturn(MerchantInfo merchant, CheckBillDelegate checkBill)
        {
            Dictionary<string, string> notifyData = ReadNotifyData();

            PayResultFactory payResultFactory = new PayResultFactory(notifyData);

            return payResultFactory.GetPayReturnDate(merchant, checkBill);
        }

        public static bool PayNotify(MerchantInfo merchant, CheckUpdateBillDelegate checkUpdateBill)
        {
            Dictionary<string, string> notifyData = ReadNotifyData();

            PayResultFactory payResultFactory = new PayResultFactory(notifyData);

            return payResultFactory.GetPayNotifyData(merchant, checkUpdateBill);
        }

        public static PayPlatformType PayPlatform()
        {
            Dictionary<string, string> notifyData = ReadNotifyData();

            PayResultFactory payResultFactory = new PayResultFactory(notifyData);

            return payResultFactory.PayPlatform();
        }

        /// <summary>
        /// 读取网关发回的数据
        /// </summary>
        private static Dictionary<string, string> ReadNotifyData()
        {

            Dictionary<string, string> notifyData = new Dictionary<string, string>();
            System.Collections.Specialized.NameValueCollection coll;
            string[] keys;

            //HttpContext.Current.Request.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            // 读取通过Get传入的值
            coll = HttpContext.Current.Request.QueryString;
            keys = coll.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                notifyData[keys[i]] = coll[keys[i]];
            }

            // 读取通过Post传入的值
            coll = HttpContext.Current.Request.Form;
            keys = coll.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                notifyData[keys[i]] = coll[keys[i]];
            }

            notifyData.Add("request_method", HttpContext.Current.Request.ServerVariables["Request_Method"].ToUpper());

            return notifyData;
        }
    }

    /// <summary>
    /// 检查银行网关页面返回对账单是否正确。（代理）
    /// </summary>
    /// <param name="bill"></param>
    /// <returns></returns>
    public delegate PayStatus CheckBillDelegate(BillInfo bill);

    /// <summary>
    /// 检查银行网关通知对账单是否正确，并更新和处理订单。银行网关支付已成功。（代理）
    /// </summary>
    /// <param name="bill"></param>
    /// <returns></returns>
    public delegate PayStatus CheckUpdateBillDelegate(BillInfo bill);
}
